package com.hejing.cloud.service.impl;

import com.hejing.cloud.apis.AccountFeignApi;
import com.hejing.cloud.apis.StorageFeignApi;
import com.hejing.cloud.entities.Order;
import com.hejing.cloud.mapper.OrderMapper;
import com.hejing.cloud.service.OrderService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

/**
 * ClassName: OrderService
 * Package: com.hejing.cloud.service
 * Description:
 *
 * @Author: HeJing
 * @Create: 2024-08-08 22:23
 * @Version: 1.0
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    // 订单微服务通过openfeign调用账户微服务
    @Resource
    private AccountFeignApi accountFeignApi;

    // 订单微服务通过openfeign调用库存微服务
    @Resource
    private StorageFeignApi storageFeignApi;

    // 此时order 既是tm也是rm
    @Override
    @GlobalTransactional(name = "hejing-create-order", rollbackFor = Exception.class)  // AT模式
    public void create(Order order) {
        // xid 全局事务id的检查，重要
        String xid = RootContext.getXID();

        // 1、新建订单
        log.info("-----------------开始新建订单: " + "\txid: " + xid);

        // 订单新建时默认的初始订单状态为0
        order.setStatus(0);

        int result = orderMapper.insertSelective(order);

        // 插入订单成功后获得插入mysql的实体对象
        Order orderFromDB = null;

        if (result > 0) {
            // 插入成功
            // 从mysql中查出来刚才插入的记录
            orderFromDB = orderMapper.selectOne(order);
            log.info("----------------------> 新建订单成功，orderFromDB info: " + orderFromDB);
            System.out.println();

            // 2、扣减库存
            log.info("----------------------> 订单微服务开始调用Storage库存，扣减count");
            storageFeignApi.decrease(orderFromDB.getProductId(), orderFromDB.getCount());
            log.info("----------------------> 订单微服务结束调用Storage库存，扣减完成");
            System.out.println();

            // 第3步、扣减账户余额
            log.info("----------------------> 订单微服务开始调用Account库存，扣减money");
            accountFeignApi.decrease(orderFromDB.getUserId(), orderFromDB.getMoney());
            log.info("----------------------> 订单微服务结束调用Account库存，扣减完成");
            System.out.println();

            // 第4步、修改订单状态为已完结（订单状态从0修改为1）
            log.info("-------------------> 修改订单状态");
            orderFromDB.setStatus(1);
            Example whereCondition = new Example(Order.class);
            Example.Criteria criteria = whereCondition.createCriteria();
            criteria.andEqualTo("userId", orderFromDB.getUserId());
            criteria.andEqualTo("status", 0);

            int updateResult = orderMapper.updateByExampleSelective(orderFromDB, whereCondition);
            log.info("-------> 修改订单状态完成" + "\t" + updateResult);
            log.info("-------> orderFromDB info: " + "\txid: " + xid);

        }
        System.out.println();
        log.info("-----------------结束新建订单: " + "\txid: " + xid);
    }
}





















